<?php
class User_Model_Location extends Zend_Db_Table{
    protected $_name = 'location';
    protected $_primary = 'ID';
    public function listLocation()
    {
        return $this->fetchAll()->toArray();
    }
	public function listHaveLocation()
    {
    	$db = Zend_Registry::get('db');
        $sql='SELECT * FROM location where lat<>0 and lat<>-1';
		$have=array();
		$have=$db->fetchAll($sql);
        return $have;
		
       // return $this->fetchAll()->toArray();
    }
	public function listNoneLocation()
    {
    	$db = Zend_Registry::get('db');
        $sql='SELECT * FROM location where lat=-1';
		$have=array();
		$have=$db->fetchAll($sql);
        return $have;
		
       // return $this->fetchAll()->toArray();
    }
	public function NameTinh($id)
	{
		$list=$this->listLocation();
		foreach ($list as $value) {
			if($value['ID']==$id){	
				return $value['Name'];
			}
		}
	}
	public function isLocation($id)
	{
		$list=$this->listLocation();
		foreach ($list as $value) {
			if($value['ID']==$id){	
				return true;
			}
		}
		return false;
	}
	public function CopyDatabase()
	{
		$db = Zend_Registry::get('db');
        $sql='SELECT * FROM location where (lat=-1 or lat=0) and id<100';
		$location =array();
		$location=$db->fetchAll($sql);
		//var_dump($location);
		//die;
		$sql='SELECT * FROM location2 where id<100';
		$location2 =array();
		$location2=$db->fetchAll($sql);
		
		foreach ($location as $value) {
			//$id=0+$value['ID'];
		//	if($id<100){
				foreach ($location2 as  $value2) {
					if(strcmp($value2['Name'] , $value['Name'])==0){
						var_dump($value2['Name']);
						var_dump($value['Name']);
						//$id2=0+$value2['ID'];
						//if($id2 <100){
						$this->updateLocation($id, $value2['lat'], $value2['lng']);
							//break;
							
					//}
					}
				}
			//}
			
		}
		return 'Pong Pi';
	}
	public function listURL()
	{
		$listurl = array();
		$list=$this->listNoneLocation();
		//var_dump($list);
		//die;
		foreach ($list as $value) {
			//if($value['lat']==0){
				$id=0+$value['ID'];
				if($id<100){
					$url= "http://maps.googleapis.com/maps/api/geocode/xml?address=".$value['Name'].",VN&sensor=true";
					
				}else if($id>100 && $id <10000){
					$idtinh=$id/100;
					
					$url= "http://maps.googleapis.com/maps/api/geocode/xml?address=".$value['Name'].",".($this->NameTinh((int)$idtinh)).",VN&sensor=true";
					
				}/*else{
					$idhuyen=$id/100;
					$idtinh=$idhuyen/100;
					$url= "http://maps.googleapis.com/maps/api/geocode/xml?address=".$value['Name'].",".($this->NameTinh((int)$idhuyen)).",".($this->NameTinh((int)$idtinh)).",VN&sensor=true";
				}*/
			$url=array('ID' => $id,'url' => $url);			
					array_push($listurl,$url);
			
			//}
		}		
		return $listurl;
	}
	public function getOneLocation($id)
    {
        $db = Zend_Registry::get('db');
        $sql='SELECT * FROM location where id=?';
        return $db->fetchRow($sql,$id);
    }
    public function updateLocation($id, $lat, $lng){
        $db = Zend_Registry::get('db');
        $where = ' ID = '.$id;
        $data = array(                    
            'lat'     => $lat,
            'lng'  => $lng
        );    
        $db->update('location', $data, $where);
    }
    public function addUser($id, $name, $lat, $lng){
        $db = Zend_Registry::get('db');
        $data = array( 
            'ID'    =>$id,                   
            'name'     => $name,
            'lat'  => $lat,
            'lng'  => $lng
        );    
        $db->insert('location',$data);
    }
	
}
?>